<template>
  <div class="app-container">
    <list-admin
      v-model="list"
      :entity-conf="entity"
      :list-display="fields"
      :list-filter="filters"
      :disabled-actions="disabled"
      :query="query"
      :config="config"
      :actions="actions"
    >
      <template v-slot:titleText>
        <span>分账记录</span>
      </template>
    </list-admin>
  </div>
</template>

<script>
import ListAdmin from '@/components/EasyAdmin/ListAdmin'
import admin from '@/config'
import axios from '@/utils/request'

export default {
  components: { ListAdmin },
  data() {
    return {
      entity: '',
      alias: '',
      list: [],
      config: {},
      fields: [],
      filters: null,
      params: this.$route.query,
      types: {
        0: '酒代分成',
        1: '管理员分成',
        2: '平台分成',
        3: '负责人分成',
        4: '场所分成',
        5: '资源公司分成',
        6: '队长分成',
        7: '经纪人分成',
        8: '提成',
        9: '补贴',
        10: '工资',
        11: '订台分成',
        12: '服务员分成',
        100: '剩余余额',
        999: '平台手续费'
      },
      settleStatus: {
        0: '未分账',
        1: '已分账'
      }
    }
  },
  created() {
    // Load entities data
    this.alias = 'OfficerSettleItem'

    if (!Object.keys(admin.entities).includes(this.alias)) {
      console.log('NO!')
    } else {
      console.log(admin.entities)
      this.config = admin.entities[this.alias]
      this.entity = Object.keys(this.config).includes('entity') ? this.config.entity : this.alias
      this.disabled = this.config.list.disabled_actions
      this.actions = this.config.list.actions
      this.query = this.config.list.query

      console.log('params', this.params)
      this.filters = {
        name: {
          expression: `entity.getSettle().getOrderItem().getSpecification().getName() matches ":value"`,
          label: '产品名称',
          type: 'input'
        },
        profitType: Object.assign({ __label: '类型' }, this.types),
        settled: Object.assign({ __label: '分账状态' }, this.settleStatus),
        'user': {
          expression: 'entity.getUser().getId() == ":value"',
          label: '业务员/工号',
          type: 'remote-select',
          data: [],
          remoteMethod: (query, context) => {
            axios
              .get(`/officer/staffs`, { params: { '@filter': `entity.getName() matches "${query}" || entity.getId() matches "${query}"` }})
              .then(res => {
                context.data = res.data.map(v => { return { value: v.user.id, label: v.id + v.name } })
              })
          }
        },
        beforeOrderCreatedTime: {
          expression: 'entity.getSettle().getOrderItem().getOrder().getCreatedTime() >= datetime.get(":value  08:00:00")',
          label: '订单开始时间',
          type: 'date'
        },
        afterOrderCreatedTime: {
          expression: 'entity.getSettle().getOrderItem().getOrder().getCreatedTime() <= datetime.get(":value 07:59:59")',
          label: '订单结束时间',
          type: 'date'
        },
        beforeCreatedTime: {
          expression: 'entity.getCreatedTime() >= datetime.get(":value  08:00:00")',
          label: '结算开始时间',
          type: 'date'
        },
        afterCreatedTime: {
          expression: 'entity.getCreatedTime() <= datetime.get(":value 07:59:59")',
          label: '结算结束时间',
          type: 'date'
        }
      }
      if (this.params.date) {
        // date范围 [0]startTime [1]endTime
        if (this.filters.beforeOrderCreatedTime) {
          this.filters.beforeOrderCreatedTime.default = this.params.date[0]
        }
        if (this.filters.afterOrderCreatedTime) {
          this.filters.afterOrderCreatedTime.default = this.params.date[1]
        }
      }
      if (this.params.profitType) {
        if (this.filters.profitType) {
          this.filters.profitType.__default = this.params.profitType.toString()
        }
      }

      this.fields = this.config.list.list_display
    }
  }
}
</script>
